<html>
<head><meta charset="utf-8"><title>how much should I worry about alignment on x86? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html">how much should I worry about alignment on x86?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="171897617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171897617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171897617">(Jul 28 2019 at 17:43)</a>:</h4>
<p>XXHash (and thus <a href="https://github.com/shepmaster/twox-hash" target="_blank" title="https://github.com/shepmaster/twox-hash">twox-hash</a>) derives a large part of its speed from treating <code>&amp;[u8]</code> as <code>&amp;[u64]</code> (or <code>&amp;[u32]</code>). I recently rewrote it to take advantage of <code>slice::align_to</code>, which made the code much nicer (hooray!). However, it also tanked the performance for unaligned input data (e.g. 10.5 Gb/sec -&gt; 4.5 Gb/sec).</p>
<p>Would it be (a) correct and (b) memory safe to ignore this alignment <em>on X86 / X86_64 only</em>, falling back to the <code>align_to</code> version on other platforms?</p>



<a name="171898324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898324">(Jul 28 2019 at 18:05)</a>:</h4>
<p>Doing an unaligned load is UB in Rust no matter the target architecture</p>



<a name="171898365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898365">(Jul 28 2019 at 18:06)</a>:</h4>
<p>we set the appropriate attributes so LLVM will optimize assuming pointers are aligned</p>



<a name="171898369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898369">(Jul 28 2019 at 18:06)</a>:</h4>
<p>I don't know exactly what this entails, <span class="user-mention" data-user-id="124289">@rkruppe</span> would likely know more -- but for now, the answer to your question is "no" on both accounts</p>



<a name="171898377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898377">(Jul 28 2019 at 18:07)</a>:</h4>
<p>are you saying that XXHash/twox-hash currently performs such unaligned loads? Or why did things get slowed with <code>align_to</code>?</p>



<a name="171898677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898677">(Jul 28 2019 at 18:17)</a>:</h4>
<p>Using <code>ptr::read_unaligned</code> instead of references/normal accesses is necessary to the avoid UB Ralf mentioned. That will kill some generic peephole optimizations which probably aren't important for this kind of code, and for x86 it shouldn't influence the final machine code (except the trivial movaps -&gt; movups if any SIMD is introduced).</p>



<a name="171898722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898722">(Jul 28 2019 at 18:18)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> I guess one question here is how costly it would be to make the "unaligned deref is UB" be a target-dependent property and <em>not</em> require this for x86</p>



<a name="171898728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898728">(Jul 28 2019 at 18:19)</a>:</h4>
<p>Making it target-dependent would be pretty gross</p>



<a name="171898889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898889">(Jul 28 2019 at 18:24)</a>:</h4>
<p>fair. I am asking mostly because I saw code in the wild in rust-lang crates (<code>rand</code>) that assumed it actually was target-dependent... so this is likely a common misconception.</p>



<a name="171898904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171898904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171898904">(Jul 28 2019 at 18:25)</a>:</h4>
<p>I've been thinking about this for a couple minutes and none of my concerns are about LLVM optimizations (well, except one which is presumably temporary and avoidable)</p>



<a name="171899077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899077">(Jul 28 2019 at 18:30)</a>:</h4>
<p>one issue with allowing it on x86 is that if people only run Miri on x86, they wouldn't get the error, thus missing this UB-- of course this applies to all target-dependent UB but this seems like a more subtle one. the issue in <code>rand</code> was only found because I ran it in Miri.</p>



<a name="171899090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899090">(Jul 28 2019 at 18:31)</a>:</h4>
<p>Some things that worry me are: the effect this will have on the ecosystem (code that's non-portable in undetectable ways -- currently, even if unaligned accesses are not "miscompiled" on x86, miri and lints and docs can steer away from it unconditionally); how people would probably request structs to be packed automatically on such targets; what that would do to type-pun-ability between atomics and normal data (since atomics do require alignment even on x86); the fact that in today's software unaligned accesses are rather limited so there might still be some non-obvious performance cliffs in some hardware</p>



<a name="171899170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899170">(Jul 28 2019 at 18:33)</a>:</h4>
<p>Oh also, I can feel in my bones that as soon as we open this pandora's box, people are going to request it as option for other targets too, including those where only some chips support unaligned loads/stores (efficiently). e.g. doing this for the baseline risc-v targets is probably not a good idea because implementations that trap &amp; emulate on unaligned accesses are encouraged, but if someone's targeting only chips that support it efficiently and natively, they'll want the non-UB as much as x86 people</p>



<a name="171899461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899461">(Jul 28 2019 at 18:43)</a>:</h4>
<blockquote>
<p>are you saying that XXHash/twox-hash currently performs such unaligned loads</p>
</blockquote>
<p>Version 1.3 <a href="https://github.com/shepmaster/twox-hash/blob/v1.3.0/src/number_streams.rs#L37-L44" target="_blank" title="https://github.com/shepmaster/twox-hash/blob/v1.3.0/src/number_streams.rs#L37-L44">took a <code>&amp;[u8]</code> and got a <code>*const u64</code></a>, then did <a href="https://github.com/shepmaster/twox-hash/blob/v1.3.0/src/number_streams.rs#L56" target="_blank" title="https://github.com/shepmaster/twox-hash/blob/v1.3.0/src/number_streams.rs#L56"><code>ptr::read</code></a>. I guess that's incorrect and should have been <code>read_unaligned</code>, but it was presumably <strong>close</strong> to being right.</p>
<p>Version 1.4 <a href="https://github.com/shepmaster/twox-hash/blob/v1.4.2/src/lib.rs#L72-L88" target="_blank" title="https://github.com/shepmaster/twox-hash/blob/v1.4.2/src/lib.rs#L72-L88">switched to <code>slice::align_to</code></a>, which should be easier to verify as correct. The problem is that the unaligned data "percolates" through the entire computation because it gets buffered internally. It can only speed up again if we finish that internal buffer and the remainder is aligned (not super likely).</p>



<a name="171899514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899514">(Jul 28 2019 at 18:44)</a>:</h4>
<p>However, <a href="https://github.com/Cyan4973/xxHash/blob/dev/xxhash.c" target="_blank" title="https://github.com/Cyan4973/xxHash/blob/dev/xxhash.c">XXHash is C code</a>. It's relatively readable, but I'm not 100% I'll state what it does correctly. I think it basically says "am I x86? if so, just cast the buffer"</p>



<a name="171899746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899746">(Jul 28 2019 at 18:51)</a>:</h4>
<p>It sounds like I need to change my abstraction to a platform-specific type that, given a <code>&amp;[u8]</code>, can return an iterator of leading <code>u8</code>, an iterator of <code>u64</code>, and an iterator of trailing <code>u8</code>. I can implement that with <code>align_to</code> on non-x86, and raw pointers and <code>read_unaligned</code> on x86.</p>



<a name="171899862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899862">(Jul 28 2019 at 18:54)</a>:</h4>
<p>I’m surprised align_to would cause a 50% perf hit, unless your test buffers are super small.</p>



<a name="171899873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899873">(Jul 28 2019 at 18:54)</a>:</h4>
<p>Aligning u8 -&gt; u64 is at most 10 cycles of instructions.</p>



<a name="171899887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899887">(Jul 28 2019 at 18:55)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> I generally agree. Those are some good arguments. Also see <a href="https://github.com/rust-lang/rust/issues/53871" target="_blank" title="https://github.com/rust-lang/rust/issues/53871">https://github.com/rust-lang/rust/issues/53871</a> and <a href="https://github.com/rust-lang/rust/issues/54915" target="_blank" title="https://github.com/rust-lang/rust/issues/54915">https://github.com/rust-lang/rust/issues/54915</a> which would be undermined by this.<br>
Though we could do what we do for overflow, which is to say that on some platforms an unaligned access will either panic or succeed -- but not cause UB.</p>



<a name="171899940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899940">(Jul 28 2019 at 18:56)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> </p>
<blockquote>
<p>Version 1.3 took a &amp;[u8] and got a *const u64, then did ptr::read. I guess that's incorrect and should have been read_unaligned, but it was presumably close to being right.</p>
</blockquote>
<p>not sure what your measure of distance is here, but this is indeed an unaligned access and hence UB. but using <code>read_unaligned</code> instead is probably an easy fix (and maybe "how hard is it to fix" was your measure of distance here?)</p>



<a name="171899996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171899996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171899996">(Jul 28 2019 at 18:58)</a>:</h4>
<p>FWIW x86 does not universally support unaligned loads/stores transparently once you get to SSE/AVX. If the compiler thinks that it should be using aligned sse load to load from an address and that address ends up being insufficiently aligned, code will not run.</p>



<a name="171900064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900064">(Jul 28 2019 at 19:00)</a>:</h4>
<p>there's also <a href="https://doc.rust-lang.org/std/primitive.pointer.html#method.align_offset-1" target="_blank" title="https://doc.rust-lang.org/std/primitive.pointer.html#method.align_offset-1">ptr::align_offset</a> which gives you the basics of <code>align_to</code> if <code>align_to</code> is overkill for your situation</p>



<a name="171900082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900082">(Jul 28 2019 at 19:01)</a>:</h4>
<p>In general I haven’t found a situation where <code>align_to</code> would be inferior to  <code>align_offset</code>. Compiler will aggressively eliminate portions of <code>align_to</code> that end up computing unnecessary stuff</p>



<a name="171900126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900126">(Jul 28 2019 at 19:02)</a>:</h4>
<p>(such as would be the case if you did not use the last slice of unaligned elements)</p>



<a name="171900214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900214">(Jul 28 2019 at 19:06)</a>:</h4>
<p>My suspicion why the perf hit occurred is that the implementation ends up calling <code>align_to</code> in a fairly hot loop..</p>



<a name="171900270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900270">(Jul 28 2019 at 19:07)</a>:</h4>
<p><a href="https://github.com/shepmaster/twox-hash/commit/da2c45433688eb1117bf84f4f3532397b089198f#diff-0fddf2104b2e6fa6ab0ec32025ee9f1bR166" target="_blank" title="https://github.com/shepmaster/twox-hash/commit/da2c45433688eb1117bf84f4f3532397b089198f#diff-0fddf2104b2e6fa6ab0ec32025ee9f1bR166">https://github.com/shepmaster/twox-hash/commit/da2c45433688eb1117bf84f4f3532397b089198f#diff-0fddf2104b2e6fa6ab0ec32025ee9f1bR166</a> for example appears to be using <code>align_to</code> mostly to "just" extract the aligned portion of the data that’s expected to be 1x4xu64.</p>



<a name="171900316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900316">(Jul 28 2019 at 19:08)</a>:</h4>
<blockquote>
<p>I’m surprised align_to would cause a 50% perf hit, unless your test buffers are super small.</p>
</blockquote>
<p>Basically, the algo wants to deal with <code>[u64; 4]</code> chunks. If we read from a <code>&amp;[u8]</code>that is one byte misaligned from that, then we slurp in X bytes to fill the buffer, then process it. The remaining data is <em>still</em> misaligned by one byte, so the process repeats.</p>
<p>In the old <code>ptr::read</code> situation, if we had data in the buffer, we could fill and finish the buffer and then treat the remainder of bytes as big chunks.</p>



<a name="171900393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900393">(Jul 28 2019 at 19:10)</a>:</h4>
<blockquote>
<p><code>read_unaligned</code> instead is probably an easy fix (and maybe "how hard is it to fix" was your measure of distance here?</p>
</blockquote>
<p>Indeed.</p>



<a name="171900400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900400">(Jul 28 2019 at 19:10)</a>:</h4>
<p>Ah, I see. Yeah, to use <code>align_to</code> well, you must have means to process the bytes in head and tail independently.</p>



<a name="171900425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900425">(Jul 28 2019 at 19:11)</a>:</h4>
<p>Right. AFAIK XXHash simply doesn't have that, instead opting to always buffer the data up. </p>
<p>Now, 5GB is nothing to sneeze at, but knowing that I'm leaving <em>another</em> 5GB/sec behind is aggravating <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="171900484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900484">(Jul 28 2019 at 19:12)</a>:</h4>
<p>I think in your case <span class="user-mention" data-user-id="116155">@Jake Goulding</span> doing a reverse is what you want to do. memcpy your bytes into <code>[u64; 4]</code> basically.</p>



<a name="171900496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900496">(Jul 28 2019 at 19:13)</a>:</h4>
<blockquote>
<p>FWIW x86 does not universally support unaligned loads/stores transparently [...] code will not run.</p>
</blockquote>
<p>Could you expand a bit on what you mean by "not run"? There are various ways I could assume that presents.</p>



<a name="171900550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900550">(Jul 28 2019 at 19:14)</a>:</h4>
<p>Do you mean to <em>always</em> copy into the buffer? I believe that's effectively what the code does now.</p>



<a name="171900552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900552">(Jul 28 2019 at 19:14)</a>:</h4>
<p>well, there're separate instructions for aligned and unaligned load/store in SSE and the CPU will trap if an aligned instruction is used with an mis-aligned address.</p>



<a name="171900572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171900572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171900572">(Jul 28 2019 at 19:15)</a>:</h4>
<p>That’s probably the most plausible way assumed-to-be-aligned-but-really-misaligned-reads-UB can manifest itself on x86.</p>



<a name="171901099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171901099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171901099">(Jul 28 2019 at 19:31)</a>:</h4>
<p>Just to clarify for myself, on x86, I can do <code>&amp;[u8] </code> -&gt; <code>*const u8</code> -&gt;  <code>*const [u64; 4]</code> and then <code>ptr::read_unaligned</code> and this shouldn't trigger UB (so long as I stay in bounds of the <code>&amp;[u8]</code>)?</p>



<a name="171901152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171901152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171901152">(Jul 28 2019 at 19:33)</a>:</h4>
<p><code>read_unaligned</code> doesn't make mention of platform-specific things; what happens if I use it on a platform that is more strict about alignment?</p>



<a name="171901157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171901157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171901157">(Jul 28 2019 at 19:33)</a>:</h4>
<blockquote>
<p>Just to clarify for myself, on x86, I can do <code>&amp;[u8] </code> -&gt; <code>*const u8</code> -&gt;  <code>*const [u64; 4]</code> and then <code>ptr::read_unaligned</code> and this shouldn't trigger UB (so long as I stay in bounds of the <code>&amp;[u8]</code>)?</p>
</blockquote>
<p>Yes, that should work.</p>



<a name="171901217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171901217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171901217">(Jul 28 2019 at 19:35)</a>:</h4>
<blockquote>
<p><code>read_unaligned</code> doesn't make mention of platform-specific things; what happens if I use it on a platform that is more strict about alignment?</p>
</blockquote>
<p>The compiler will have to split reads into aligned parts and stitch the result back into what you want. This could be done as a memcpy under the covers.</p>



<a name="171901268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171901268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171901268">(Jul 28 2019 at 19:36)</a>:</h4>
<p>hmm. So if I can't do something better than what the compiler does, it might be better for me to use <code>read_unaligned</code> on all the platforms anyway...?</p>



<a name="171901392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171901392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171901392">(Jul 28 2019 at 19:40)</a>:</h4>
<p>Since I'm basically buffering unaligned data in a way that is reminiscent of memcpy</p>



<a name="171905244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171905244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171905244">(Jul 28 2019 at 21:35)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> probably. If you had no bounds on space, you could allocate a properly aligned buffer and copy everything in one go</p>



<a name="171905249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171905249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171905249">(Jul 28 2019 at 21:35)</a>:</h4>
<p>which would be significantly faster in certain scenarios</p>



<a name="171905515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171905515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171905515">(Jul 28 2019 at 21:43)</a>:</h4>
<p>seems like a bad API for this case if you have to re-align every 32-byte buffer, instead of being able to process some prefix and then a sequence of properly aligned 32-byte buffers</p>



<a name="171905560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171905560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171905560">(Jul 28 2019 at 21:44)</a>:</h4>
<p>I mean the algorithm already has to support smaller chunks, right? what if I hash a <code>u16</code>?</p>



<a name="171907391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171907391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171907391">(Jul 28 2019 at 22:41)</a>:</h4>
<blockquote>
<p>if you have to re-align every 32-byte buffer</p>
</blockquote>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> certainly, but as I understand it, the algorithm itself was designed assuming x86 semantics. Thus the API itself has some of these assumptions baked-in. The algorithm effectively requires that it be cheap to "realign"  </p>
<blockquote>
<p>what if I hash a <code>u16</code>?</p>
</blockquote>
<p>That's fine, but there's special code both for "finalizing" the hash and for the specific case where you only hashed data of length less-than the internal buffer. AFAIK, we can't just "finish" the buffer and then keep adding on afterwards.</p>



<a name="171907435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171907435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171907435">(Jul 28 2019 at 22:42)</a>:</h4>
<blockquote>
<p>allocate a properly aligned buffer and copy everything in one go</p>
</blockquote>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> I should probably add that to the documentation for those on other architectures.</p>



<a name="171910481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20much%20should%20I%20worry%20about%20alignment%20on%20x86%3F/near/171910481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20much.20should.20I.20worry.20about.20alignment.20on.20x86.3F.html#171910481">(Jul 29 2019 at 00:26)</a>:</h4>
<p>Thank you, RalfJ, rkruppe, and nagisa!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>